package jerry.html.data.convertors.factory;

import java.util.Map;
import java.util.ResourceBundle;

import jerry.html.LogParser;
import jerry.html.data.accessors.DataAccessor;
import jerry.html.data.convertors.DataConvertor;
import jerry.html.events.EvtCap;
/**
 * 
 * Creator class to fetch the convertor for a particular data type
 * 
 * 
 * @author (h0t@_G0|i
 * 
 */
public class DataConvertorFactory {
	private DataConvertorFactory() {}
	private static final ResourceBundle convertors = ResourceBundle.getBundle("dataConvertors");
	private static DataConvertorFactory factoryInstance;
	public static DataConvertorFactory getFactoryInstance() {
		if(factoryInstance == null) {
			factoryInstance = new DataConvertorFactory();
		}
		return factoryInstance;
	}
	
	public DataConvertor getDataConvertor(String dataType, Map<String, DataAccessor> dataAccessors) {
		String evtClass = LogParser.classes.getString(dataType);
		try {
			String className = convertors.getString(dataType);
			DataConvertor newInstance = (DataConvertor) Class.forName(className).newInstance();
			newInstance.setDataType(Class.forName(evtClass).asSubclass(EvtCap.class));
			newInstance.setAccessors(dataAccessors);
			return newInstance;
		} catch (Exception e) {
			System.err.println("Problem while getting convertor for the passed datatype : " + evtClass);
		}
		return null;
	}
}
